package com.vhr.service.impl;

import com.vhr.VO.DimissionAdminVO;
import com.vhr.entities.*;
import com.vhr.mapper.AccountMapper;
import com.vhr.mapper.DepartmentMapper;
import com.vhr.mapper.DimissionMapper;
import com.vhr.service.IDimissionService;
import com.vhr.service.IEmployeeService;
import com.vhr.util.MonthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IDimissionServiceImpl implements IDimissionService{

    @Autowired
    DimissionMapper dimissionMapper;

    @Autowired
    IDimissionService iDimissionService;

    @Autowired
    IEmployeeService iEmployeeService;

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    DepartmentMapper departmentMapper;

    //查询所有离职申请
    @Override
    public List<Dimission> selectAll() {
        return dimissionMapper.selectByExample(null);
    }

    //查询所有未审批的离职申请
    @Override
    public List<Dimission> selectWithoutJudge() {
        DimissionExample dimissionExample = new DimissionExample();
        dimissionExample.createCriteria().andDimissionStatusEqualTo(0);
        List<Dimission> list = dimissionMapper.selectByExample(dimissionExample);
        return list;
    }

    //根据员工外键查询员工离职信息
    @Override
    public List<Dimission> selectByEmployee(Employee emp) {
        DimissionExample dimissionExample = new DimissionExample();
        dimissionExample.createCriteria().andEmpIdEqualTo(emp.getEmpId());
        List<Dimission> list = dimissionMapper.selectByExample(dimissionExample);
        return list;
    }

    //插入离职信息（申请离职）
    @Override
    @Transactional//Spring事务
    public int addDimission(Dimission dimission) {
        dimission.setDimissionTime(MonthUtil.MonthDecrease(dimission.getDimissionTime()));
        return dimissionMapper.insertSelective(dimission);
    }

    //审批离职
    @Override
    @Transactional//Spring事务
    public int judegDimission(Dimission dimission,Employee admin,Integer status) {
        dimission.setDimissionStatus(status);
        dimission.setJudgeId(admin.getEmpId());
        int result = dimissionMapper.updateByPrimaryKeySelective(dimission);
        return result;
    }

    //根据主键更改信息
    @Override
    public int updateByPrimary(Dimission dimission) {
        dimission.setDimissionTime(MonthUtil.MonthDecrease(dimission.getDimissionTime()));
        return dimissionMapper.updateByPrimaryKeySelective(dimission);
    }

    //根据主键删除信息
    @Override
    public int deleteByPrimary(Dimission dimission) {
        return dimissionMapper.deleteByPrimaryKey(dimission.getDimissionId());
    }

    /*管理员：查询所有离职申请*/
    @Override
    public List<DimissionAdminVO> AdminSelectAll() {
        /*
        * 思路：关联地查出不同表的数据，再封装成扩展类
        * 1.查dimission表，获取4个属性
        * 2.根据dimission表的empId查employee表的出工号和姓名
        * 3.根据dimission表的empId查employee表的所属部门
        * */
        //类
        List<DimissionAdminVO> VOList = new ArrayList<>();

        //查dimission表
        List<Dimission> list = iDimissionService.selectAll();

        for(int i=0;i<list.size();i++){
            DimissionAdminVO vo = new DimissionAdminVO();
            Employee employee = new Employee();
            //离职id
            vo.setDimissionId(list.get(i).getDimissionId());
            //离职时间
            Date date = list.get(i).getDimissionTime();
            DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(date);
            vo.setDimissionTime(time);
            //理由
            vo.setDimissionReason(list.get(i).getDimissionReason());
            //状态（0未审批，1通过，2未通过）
            vo.setDimissionStatus(list.get(i).getDimissionStatus());
            //姓名
            employee.setEmpId(list.get(i).getEmpId());//i=0,empId=2,dimissionId=1
            Employee emp = iEmployeeService.selectEmployeeByPrimaryKey(employee);
            vo.setEmpName(emp.getEmpName());
            //工号
            AccountExample accountExample = new AccountExample();
            AccountExample.Criteria criteria = accountExample.createCriteria();
            criteria.andEmpIdEqualTo(emp.getEmpId());//i=0,empId=2,dimissionId=1
            List<Account> listAccount = accountMapper.selectByExample(accountExample);
            vo.setEmpNum(listAccount.get(0).getEmpNum());
            //所属部门
            Department department = departmentMapper.selectByPrimaryKey(emp.getDeptId());
            vo.setDeptName(department.getDeptName());
            //System.out.println(vo.toString());

            VOList.add(vo);
        }
        return VOList;
    }

    //管理员：查询所有已经审批过的加班申请，1通过，2未通过
    @Override
    public List<DimissionAdminVO> selectOperated() {
        return dimissionMapper.selectOperated();
    }

    //管理员：查询管理员未审批的加班申请
    @Override
    public List<DimissionAdminVO> selectNoOperated() {
        return dimissionMapper.selectNoOperated();
    }

    //管理员:条件模糊搜索查询离职申请记录
    @Override
    public List<DimissionAdminVO> listDimissionByCondition(Integer deptId, String empName, Integer dimissionStatus) {
        return dimissionMapper.listDimissionByCondition(deptId,empName,dimissionStatus);
    }
}
